package com.dcdata.xian;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class ClsMainClient {

    private static String fileName;
    private static FileOutputStream fos;
    private static DataOutputStream outStream;

    private static boolean firstTime = true;

    public static void main(String[] args) {

        generateFileNameSchedule();

        if (fileName == null || fileName.isEmpty()) {
            LocalDate localDate = LocalDate.now();
            fileName = System.getProperty("user.dir") + "/data/gps/GPS" + localDate.toString().replace("-", "_") + ".txt";
        }
        try {
            fos = new FileOutputStream(fileName);
            outStream = new DataOutputStream(new BufferedOutputStream(fos));
        } catch (Exception e) {
            System.out.println("写文件内容操作出错");
            e.printStackTrace();
        }

        TcpClient c1 = new TcpClient() {

            @Override
            public void onReceive(SocketTransceiver st, String s) {
                try {
                    outStream.writeUTF(s + "\n");
                } catch (Exception e) {
                    System.out.println("写文件内容操作出错");
                    e.printStackTrace();
                }
            }

            @Override
            public void onDisconnect(SocketTransceiver st) {
                try {
                    if (outStream != null){
                        outStream.close();
                    }
                    outStream = new DataOutputStream(new BufferedOutputStream(fos));
                } catch (Exception e) {
                    System.out.println("写文件内容操作出错");
                    e.printStackTrace();
                }
                System.out.println("Client Disconnect");
                delay();
                System.out.println("Client will start reconnect...");
                this.connect("10.238.161.156", 18183);
            }

            @Override
            public void onConnect(SocketTransceiver transceiver) {
                System.out.println("Client Connect");
            }

            @Override
            public void onConnectFailed() {
                try {
                    if (outStream != null){
                        outStream.close();
                    }
                    outStream = new DataOutputStream(new BufferedOutputStream(fos));
                } catch (Exception e) {
                    System.out.println("写文件内容操作出错");
                    e.printStackTrace();
                }
                System.out.println("Client Connect Failed");
                delay();
                System.out.println("Client will start reconnect...");
                this.connect("10.238.161.156", 18183);
            }
        };

        c1.connect("10.238.161.156", 18183);

        delay();

        firstTime = false;
        while (true) {
            if (c1.isConnected()) {

            } else {
                break;
            }
            delay();

        }
    }

    private static void generateFileNameSchedule() {
        File dataDir = new File(System.getProperty("user.dir") + "/data");
        if (!dataDir.exists() && !dataDir.isDirectory())
            if (!dataDir.mkdir()) {
                System.out.println("mkdir data failed");
            }

        File gpsDir = new File(System.getProperty("user.dir") + "/data/gps");
        if (!gpsDir.exists() && !gpsDir.isDirectory())
            if (!gpsDir.mkdir()) {
                System.out.println("mkdir data/gps failed");
            }

        Timer timer = new Timer();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, Config.GPS_HOUR); //4点
        calendar.set(Calendar.MINUTE, Config.GPS_MINUTE);//45分
        calendar.set(Calendar.SECOND, Config.GPS_SECOND);//0秒
        //每天的凌晨4:45,重新生成新文件
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                //每天定时生成新的文件路径和相应的输出流
                if(firstTime == false) {
                    LocalDate localDate = LocalDate.now();
                    fileName = System.getProperty("user.dir") + "/data/gps/GPS" + localDate.toString().replace("-", "_") + ".txt";

                    try {
                        if (outStream != null){
                            outStream.close();
                        }
                        fos = new FileOutputStream(fileName);
                        outStream = new DataOutputStream(new BufferedOutputStream(fos));
                    } catch (Exception e) {
                        System.out.println("写文件内容操作出错");
                        e.printStackTrace();
                    }
                }
            }
        }, calendar.getTime(), 24 * 60 * 60 * 1000);
    }

    private static void writeLocalGPS(String response) {
        try {
            outStream.writeUTF(response + "\n");
        } catch (Exception e) {
            System.out.println("写文件内容操作出错");
            e.printStackTrace();
        }
    }


    static void delay() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
